/* Demo for input iterator and output iterator 
   <<C++ Standard Library, A Tutorial and Reference.pdf>>
   Page 220.
*/

#include <algorithm>
#include <iterator>
#include <ostream>
#include <iostream>
#include <fstream>

using namespace std ;

void f ( )
{
	ofstream ints ("someInts.dat") ;
	ints << "1 2 3  4 5 6 7 8 9 10 a b c d e f g h i j k" ;
	ints.close();
	ifstream inf("someInts.dat");

	istream_iterator<int> isi1(inf);
	istream_iterator<int> isi2(isi1);
	
	ostream_iterator<int> osi1(cout, " ");
	ostream_iterator<int> osi2(osi1);

	for(int i=0; i < 4; i++)
		*(++osi1) = *(++isi1); //All the related input interator
							   //and output interator(osi2 and isi2)
							   //steps over along with osi1 and isi1.
	//isi2 remains old value before ++.
	cout<< endl << "*isi2=" << *isi2 << endl;

	//isi2 = isi1 after ++.
	for(int i=0; i < 4; i++)
		*(++osi2) = *(++isi2);

/*	*osi1 = *isi1++ ;
	*osi2 = *isi2++ ;
	*osi2 = *isi2++ ;
	*osi1 = *isi1++ ;
*/
	cout<< endl ;
	inf.close();
}
	
void g ( )
{
	ifstream inf("someInts.dat");
	ofstream outInts ("outInts.dat");
	istream_iterator<int> isi1(inf);
	istream_iterator<int> isi2(isi1);
	
	ostream_iterator<int> osi1(outInts, " ");
	ostream_iterator<int> osi2(osi1);
	
	*osi1 = *isi1++ ;
	*osi2 = *isi2++ ;
	*osi2 = *isi2++ ;
	*osi1 = *isi1++ ;

}

int main ( )
{
	cout<< "====== f =====" << endl;
	f();
	cout<< "====== g =====" << endl;
	g();
	cout<< endl ;

	return 0 ;
}
